package com.mz.ozy.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.activity.MainActivity;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.LoginBean;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.BindPhonePresenter;
import com.mz.ozy.mvp.presenter.CodePresenter;
import com.mz.ozy.mvp.presenter.LoginPresenter;
import com.mz.ozy.mvp.view.BindPhoneContract;
import com.mz.ozy.mvp.view.CodeContract;
import com.mz.ozy.utils.CodeHelper;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.SharePreferencesData;
import com.mz.ozy.utils.ToastUtils;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 绑定手机
 */
public class BindPhoneActivity extends BaseActivity<BindPhoneContract.View, BindPhonePresenter> implements BindPhoneContract.View, CodeContract.View {

    private static PhoneSmsLoginActivity sActivity;
    @BindView(R.id.etPhone)
    EditText mEtPhone;
    @BindView(R.id.etCode)
    EditText mEtCode;
    @BindView(R.id.tv_get_code)
    TextView mTvGetCode;
    @BindView(R.id.bt_next)
    Button mBtNext;

    private CodeHelper codeHelper;
    private CodePresenter mCodePresenter;
    private int type;
    private String id;

    public static void go(PhoneSmsLoginActivity activity, int type, String id) {
        sActivity = activity;
        Intent intent = new Intent(activity, BindPhoneActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("id", id);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_phone);
        ButterKnife.bind(this);
        initTitle();
        initData();
    }

    private void initData() {
        type = getIntent().getIntExtra("type", LoginPresenter.AUTH_TYPE_WX);
        id = getIntent().getStringExtra("id");
        mEtPhone.setText(SPUtils.getInstance(this).getString(Constants.SPName.USERNAME, ""));
        codeHelper = new CodeHelper(this, mTvGetCode);
        mCodePresenter = new CodePresenter(this);
    }


    @OnClick({R.id.tv_get_code, R.id.bt_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                String phone = mEtPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone) || phone.length() != 11) {
                    ToastUtils.showToast("请输入合法的手机号码");
                    return;
                }
                mCodePresenter.onGetCode(this, phone, CodePresenter.MSG_TYPE_BIND);
                break;
            case R.id.bt_next:
                tryNext();
                break;
        }
    }

    private void tryNext() {
        String phone = mEtPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone) || phone.length() != 11) {
            ToastUtils.showToast("请输入合法的手机号码");
            return;
        }
        String code = mEtCode.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            ToastUtils.showToast("验证码不能为空!");
            return;
        }
        mPresenter.onBind(this, type, phone, code, id);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCodePresenter.detachView();
        mCodePresenter = null;
    }

    @Override
    public void onGetCodeSuccess(JSONObject jsonObject) {
        ToastUtils.showToast(getString(R.string.have_send_verify_code));
        codeHelper.start();  //开启倒计时
    }

    @Override
    public void onGetCodeFail() {
        codeHelper.reset();  //可以点击
    }

    @Override
    public void handleFailResponse(NetBean netBean) {
        super.handleFailResponse(netBean);
        onGetCodeFail();
    }

    /**
     * 执行登录成功逻辑
     *
     * @param loginBean
     */
    @Override
    public void onBindSuccess(LoginBean loginBean) {
//        判断是否设置密码
        SPUtils.getInstance(this).putInt("paynum", loginBean.getPaynum());
        SPUtils.getInstance(this).putString(Constants.SPName.IS_LOGIN, "1");
        if (SPUtils.getInstance(this).getString(Constants.PREE.IS_PAY_PWD, "").equals("1")) {  //是否设置密码
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            Intent intent = new Intent(this, SetPasswordActivity.class);
            startActivity(intent);
            finish();
        }
        if (sActivity != null) {
            sActivity.finish();
        }
    }
}
